home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9294 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Standard question - pointer initialization
  5. Date: 8 Mar 1996 13:41:16 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hq9hsINN998@keats.ugrad.cs.ubc.ca>
  8. References: <4hk9un$906@hammer.msfc.nasa.gov> <4hl6rr$nde@news.xs4all.nl> <313E6028.1C19@ix.netcom.com> <4hnpsl$g8c@hacgate2.hac.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hnpsl$g8c@hacgate2.hac.com>,
  12. Ron Collins <collins@thor.tu.hac.com> wrote:
  13. >Norman Bullen (nbullen@ix.netcom.com) wrote:
  14. >[snip]
  15. >
  16. >: And there is no point in initializing a pointer when its declared if you 
  17. >: don't know at that point what an appropriate value might be. Use of a 
  18. >: pointer that has been initialized with an inappropriate value can be just 
  19. >: as bad for the program as using an uninitialize pointer.
  20. >
  21. >The popular convention (when initializing pointers) is to set them to
  22. >NULL.  This is the appropriate value to use when checking for invalid
  23. >pointer usage.
  24.  
  25. This is not a popular convention, but something that is demanded by the
  26. language standard. A static uninitialized pointer shall take on a value that
  27. corresponds to a null pointer, whatever that bit pattern is.
  28.  
  29. Also note that NULL is just a macro that stands for the value zero (often
  30. accompanied by a cast to void *). In assigning default initialization values to
  31. static variables, the compiler couldn't care less that there is a pre-processor
  32. macro called NULL. NULL can never be anything other than zero in a
  33. standard-conforming implementation of the C language.
  34.  
  35. Under any C implementation, a constant integral expression having zero value
  36. produces a null pointer value in a context where a pointer is required,
  37. regardless of the physical representation of a null pointer required by the
  38. environment. Thus checking for a null pointer p by using ``if (!p)'' isn't any
  39. better or worse than ``if (p == NULL)'' (after inclusion of an appropriate
  40. standard header). Even on some imaginary computer where pointers are
  41. represented by some kind of associative look-up identifier, a standard C
  42. implementation would still ensure that the statement ``p = 0;'' turns p into a
  43. null pointer.
  44.  
  45. By the way, null pointers are not for checking for incorrect pointer usage. It
  46. is obviously legal and useful for a pointer to have a null value. Such a value
  47. is guaranteed to always be distinct from a pointer which references an actual
  48. object.
  49. -- 
  50.  
  51.